﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Restaurant.DTO;
using Restaurant.DAO;

namespace Restaurant.BUS
{
    public class MonAnService
    {
        private MonAnDB maDAO = new MonAnDB();

        public DataTable LoadMonAn()
        {
            return maDAO.LoadMonAn();
        }

        public bool XoaMonAn(int ma)
        {
            if (maDAO.XoaCTMonAn(ma))
            {
                maDAO.XoaMonAn(ma);
                return true;
            }
            return false;
        }

        public DataTable LoadLoaiMonAn()
        {
            return maDAO.LoadLoaiMonAn();
        }

        public int LayMaLoaiTheoTen(string ten)
        {
            if (ten != "")
            {
                DataTable dbtab = maDAO.LayMaLoaiTheoTen(ten);
                if (dbtab.Rows.Count > 0)
                {
                    DataRow dbrow = dbtab.Rows[0];
                    return int.Parse(dbrow["MaLoaiMonAn"].ToString());
                }
            }
            return -1;
        }

        public bool ThemMonAn(MonAn ma, List<NguyenLieu> listNL)
        {
            int mamonan = 0;
            if (ma.Tenmon == "" || ma.Dongia.ToString() == "" || ma.Maloai.ToString() == "")
                return false;
            if (maDAO.ThemMonAn(ma.Tenmon, ma.Dongia, ma.Maloai,ref mamonan))
            {
                if (mamonan != 0)
                {
                    for (int i = 0; i < listNL.Count; i++)
                    {
                        if (!maDAO.ThemCTMonAn(mamonan, listNL[i].MaNL, listNL[i].Soluong))
                        {
                            maDAO.XoaCTMonAn(mamonan);
                            maDAO.XoaMonAn(mamonan);
                            return false;
                        }
                    }
                    return true;
                }
                else
                    return false;
            }
            else
                return false;
        }

        public MonAn TimMonAn(int ma)
        {
            DataTable dbtab = maDAO.TimMonAn(ma);
            MonAn monan = new MonAn();
            if (dbtab.Rows.Count > 0)
            {
                DataRow dbrow = dbtab.Rows[0];

                monan.Mamonan = int.Parse(dbrow["MaMonAn"].ToString());
                monan.Tenmon = dbrow["TenMonAn"].ToString();
                monan.Maloai = int.Parse(dbrow["MaLoaiMonAn"].ToString());
                monan.Dongia = int.Parse(dbrow["DonGia"].ToString());
                return monan;
            }
            monan.Mamonan = -1;
            return monan;
        }

        public string LayTenLoaiTheoMa(int ma)
        {
            if (ma.ToString() != "")
            {
                DataTable dbtab = maDAO.LayTenLoaiTheoMa(ma);
                if (dbtab.Rows.Count > 0)
                {
                    DataRow dbrow = dbtab.Rows[0];
                    return dbrow["TenLoaiMonAn"].ToString();
                }
            }
            return "";
        }

        public DataTable LayCTMonAn(int ma)
        {
            return maDAO.LayCTMonAn(ma);
        }

        public bool SuaMonAn(MonAn ma, List<NguyenLieu> listNL)
        {
            if (ma.Tenmon == "" || ma.Dongia.ToString() == "" || ma.Maloai.ToString() == "")
                return false;
            if (maDAO.SuaMonAn(ma.Tenmon, ma.Dongia, ma.Maloai, ma.Mamonan))
            {
                if (ma.Mamonan != 0)
                {
                    maDAO.XoaCTMonAn(ma.Mamonan);
                    for (int i = 0; i < listNL.Count; i++)
                    {
                        maDAO.ThemCTMonAn(ma.Mamonan,listNL[i].MaNL,listNL[i].Soluong);
                    }
                    return true;
                }
                else
                    return false;
            }
            else
                return false;
        }
    }
}
